/**
 * Created by Arterli on 2015/4/26.
 */
module.exports=Controller("Admin/BaseController", function(){
   "use strict";
    return {
        indexAction:function(){
            this.assign({
                active:"tag"
            });
            this.display();
        },
        dataAction:function(){
           var self=this;
            var page = this.get("offset") > 1 ? (this.get("offset") / 20) + 1 : 1;
            var limit = this.get("limit");
            var search = this.get('search').length > 0 ? this.get('search') : "";
            var where={tagname: ['like', '%' + search + '%']};
            return D('tag').page(page,limit).order('tid desc').where(where).countSelect().then(function(tag){
                var data={};
                console.log(tag)
                data.total = tag.count;
                var rows=[];
                tag.data.forEach(function(item){
                    rows.push({
                        tid: item.tid,
                        tagname:'<a class="view ml10" href="'+item.tid+'" title="访问"><i class="glyphicon glyphicon-link"></i></a> '+ item.tagname,
                        gid: item.gid
                    });
                });
                data.rows = rows;
                self.json(data);
            })
        },//TODO xiugai shanchu

        //tag 标签查询
        tagQueryAction: function(){
            //data.query.results.toplevel.CompleteSuggestion       {"suggestion":{"data":"clayton homes"}}
            var self = this;
            var term = self.get('term');
            var sql = ""+
            "SELECT tagname FROM(SELECT * ,ELT(INTERVAL(CONV(HEX(left(CONVERT(tagname USING gbk),1)),16,10)," +
            "0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1)," +
            "'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z') as py FROM __TABLE__)as ss WHERE py = UPPER('%s') ";

            return D('tag').query(sql, term).then(function(tag){
                var arr = [];
                //console.log(tag);
                tag.forEach(function( item ){
                    arr.push({"suggestion":{"data":item.tagname }});
                });
                var created = new Date();
                var res = {"query":{"count":1,"created":created,"lang":"zh-CN","results":{"toplevel":{"CompleteSuggestion":arr}}}};
                self.json(res);
            });
        }
    }
});
